package at.bartinger.gameoflive.otherstuff;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import at.bartinger.gameoflive.R;
import at.bartinger.gameoflive.R.id;
import at.bartinger.gameoflive.R.layout;


public class IntSetterDialog extends Dialog{

	private int number;
	private OkListener l;
	private EditText text;
	private boolean onceSet = false;
	private Context context;
	private DisplayMetrics dm;

	public IntSetterDialog(Context context, String title,int defaultnumber,OkListener l, DisplayMetrics dm) {
		super(context);
		setContentView(R.layout.intsetter); 
		setTitle(title);
		this.l = l;
		this.context = context;
		this.dm = dm;
		Button ok = (Button)findViewById(R.id.ok);
		ok.setOnClickListener(new OKListenerObj());

		text = (EditText) findViewById(R.id.textnumber);
		text.setText(""+defaultnumber);
	}

	private class OKListenerObj implements android.view.View.OnClickListener { 

		@Override 
		public void onClick(View v) {
			String str = text.getText().toString();
			boolean containsAlph = false;
			for (int i = 0; i < str.length(); i++) {

				//If we find a non-digit character we return false.
				if (!Character.isDigit(str.charAt(i))){
					containsAlph = true;
					break;
				}

			}
			if(!containsAlph){
				number = Integer.parseInt(str);
				if(number < dm.heightPixels || number < dm.widthPixels){
					l.onOkPressed(number);
					IntSetterDialog.this.dismiss();
					onceSet = true;
				}else{
					Toast.makeText(context, "The size must be smaller than the reselution of your phone ("+dm.widthPixels+"x"+dm.heightPixels+")", Toast.LENGTH_LONG).show();
				}
			}else{
				Toast.makeText(context, "Please just insert numbers!", Toast.LENGTH_LONG).show();
			}
		}

	}

	public interface OkListener{
		public void onOkPressed(int setnumber);
	}

	@Override
	public void show() {
		if(onceSet)
			text.setText(""+number);
		super.show();
	}

}
